package com.myccb.app.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.myccb.app.AppStatus;
import com.myccb.app.view.activity.LoginActivity;
import com.myccb.app.R;
import com.myccb.app.base.BaseFragment;
import com.myccb.app.util.DateTimeUtil;
import com.myccb.app.widget.rollpageview.ColorPointHintView;
import com.myccb.app.widget.rollpageview.LoopPagerAdapter;
import com.myccb.app.widget.rollpageview.RollPagerView;

/**
 * Created by hyt on 2017/11/24.
 */

public class LoanFragment extends BaseFragment {

    private static LoanFragment instance;
    private ImageView refreshBtn;
    private TextView sumLoan;
    private TextView refreshTime;

    private TextView welcomeView;
    private TextView queryLoanBtn;


    private LinearLayout headerLayoutNologin;
    private RelativeLayout headerLayoutLogin;
    private boolean hideBalance;


    public static LoanFragment getInstance(){
        if (instance==null)
            instance=new LoanFragment();
        return instance;
    }

    @Override
    public void onResume() {
        if (isCreate()){
            super.onResume();
            initData();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_loan, null);
        initView(view);
        initData();
        return view;
    }
    private void initView(View view){
        RollPagerView adRollPageView=view.findViewById(R.id.loan_mainpage_ads);
        adRollPageView.setHintView(new ColorPointHintView(getContext(),getResources().getColor(R.color.colorPrimary),getResources().getColor(R.color.Grey200)));

        adRollPageView.setAdapter(new LoopPagerAdapter(adRollPageView) {
            private int[] adImgs={
                    R.mipmap.loan_mainpage_adv_1, R.mipmap.loan_mainpage_adv_2
            };
            @Override
            public View getView(ViewGroup container, int position) {
                ImageView view = new ImageView(container.getContext());
                view.setImageResource(adImgs[position]);
                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                return view;
            }
            @Override
            public int getRealCount() {
                return adImgs.length;
            }
        });


        refreshBtn=view.findViewById(R.id.refresh_info);
        refreshBtn.setPivotX(refreshBtn.getWidth()/2);
        refreshBtn.setPivotY(refreshBtn.getHeight()/2);

        headerLayoutNologin=view.findViewById(R.id.loan_header_nologin);
        headerLayoutLogin=view.findViewById(R.id.loan_header_login);

        welcomeView=view.findViewById(R.id.loan_welcome_view);
        queryLoanBtn=view.findViewById(R.id.query_loan_btn);

        queryLoanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });

        final CheckBox hideBtn=view.findViewById(R.id.checkbox_hide_sum_money);
        sumLoan=view.findViewById(R.id.sum_loan);
        refreshTime=view.findViewById(R.id.last_refresh_time);
        //BCNumView=view.findViewById(R.id.lab_sum_loan);
        //BCNumView.setText("******");

        refreshBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshBtnRotate();
                //new IndexFragment.GetUserInfoTask().execute(2);
                //refreshBtn.setRotation(360);
            }
        });

        hideBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                hideBalance=!isChecked;
                if (isChecked)
                    sumLoan.setText("---");
                else
                    sumLoan.setText("****");
            }
        });
    }

    private void initData(){
        if (AppStatus.isLogin){
            switchHeaderLayout(1);
            //new IndexFragment.GetUserInfoTask().execute(0);
        }
        else if (AppStatus.getInstance().isSaveUserInfo()){
            switchHeaderLayout(0);
            welcomeView.setText(DateTimeUtil.getCurrentPeriodOfDay()+"好！"+AppStatus.getInstance().getSavedUserName());
            queryLoanBtn.setText("查看贷款");
        }
        else {
            switchHeaderLayout(0);
            welcomeView.setText(DateTimeUtil.getCurrentPeriodOfDay()+"好！");
            queryLoanBtn.setText("马上登陆");
        }
    }
    private void switchHeaderLayout(int mode){
        switch (mode){
            case 0:
                headerLayoutNologin.setVisibility(View.VISIBLE);
                headerLayoutLogin.setVisibility(View.INVISIBLE);
                break;
            case 1:
                headerLayoutNologin.setVisibility(View.INVISIBLE);
                headerLayoutLogin.setVisibility(View.VISIBLE);
                break;
        }

    }
    private void refreshBtnRotate(){
        //SimpleDateFormat formatter=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        //Date curDate =  new Date(System.currentTimeMillis());
        refreshTime.setText(DateTimeUtil.getCurrentDateTime("yyyy/MM/dd HH:mm:ss")/*formatter.format(curDate)*/);
        Animation rotateAnimation  = new RotateAnimation(0,720,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setDuration(1000);
        rotateAnimation.setRepeatCount(0);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        refreshBtn.startAnimation(rotateAnimation);
    }
}
